package com.zyh.server.utils;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 给前端统一返回的结果，针对业务层的所有方法实现都返回该对象
 */
@Data
@Accessors(chain = true)  //开启链式编程，重要
public class R{
    /**
     * 状态码，为0表示成功状态
     */
    private Integer code;
    //状态
    private boolean success;
    //返回文字描述
    private String msg;
    //传递给前端的数据
    private Object data;

    /**
     * 封装统一成功返回结果
     */
    public static R ok() {
        R r = new R();
        r.code = ResponseCode.Success.getCode();
        r.success=true;
        return r;
    }

    public static R ok(String msg) {
	    R r = ok();
        r.msg = msg;
        return r;
    }

    /**
     * 封装统一失败返回结果
     */
    public static R fail() {
        R r = new R();
        r.code = ResponseCode.Error.getCode();
        r.success=false;
        r.msg = ResponseCode.Error.getDesc();
        return r;
    }

    public static R fail(String msg) {
        R r = new R();
        r.code = ResponseCode.Error.getCode();
        r.success=false;
        r.msg = msg;
        return r;
    }

}
